package com.heeexy.example.service;

import com.alibaba.fastjson.JSONObject;
import com.heeexy.example.dao.FilesDao;
import com.heeexy.example.dto.FilesEntity;
import com.heeexy.example.util.CommonUtil;
import com.heeexy.example.util.FileUtil;
import com.heeexy.example.util.constants.ErrorEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ResourceUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @Author MyPC
 * @Date 2022/3/31 13:52
 * @description：
 * @Version 1.0
 */
@Slf4j
@Service
public class FilesService {

    private FilesDao filesDao;

    @Autowired
    public void setFilesDao(FilesDao filesDao) {
        this.filesDao = filesDao;
    }

    /**
     * 添加素材资源
     * @param uid file
     * @return
     */
    @Transactional
    public JSONObject addFiles(MultipartFile file,String uid,String lid,String realPath){
        try{
            File dateDir = FileUtil.mkdirsPath(realPath);
            FilesEntity filesEntity = new FilesEntity();
            addFileEntity(uid, lid, dateDir, filesEntity, file);
            filesEntity.setHot(0);
            filesDao.saveFile(filesEntity);
        }catch (IOException e){
            log.error("文件存储异常");
            return CommonUtil.errorJson(ErrorEnum.E_400);
        }

        return CommonUtil.successJson();
    }
    /**
     * 批量添加素材资源
     * @param uid file
     * @return
     */
    @Transactional
    public JSONObject addListFiles(List<MultipartFile> fileList, String uid, String lid,String realPath){
        try{
            File dateDir = FileUtil.mkdirsPath(realPath);
            List<FilesEntity> filesEntityList = new ArrayList<FilesEntity>();
            FilesEntity filesEntity = null;
            for (MultipartFile file:fileList) {
                filesEntity = new FilesEntity();
                addFileEntity(uid, lid, dateDir, filesEntity, file);
                filesEntityList.add(filesEntity);
            }
            filesDao.saveBatchFile(filesEntityList);
        }catch (IOException e){
            log.error("文件存储异常");
            return CommonUtil.errorJson(ErrorEnum.E_400);
        }

        return CommonUtil.successJson();
    }

    /**
     * 将文件存到文件资源目录
     * @param uid
     * @param lid
     * @param dateDir
     * @param filesEntity
     * @param file
     * @throws IOException
     */
    private void addFileEntity(String uid, String lid, File dateDir, FilesEntity filesEntity, MultipartFile file) throws IOException {
        filesEntity.setLId(!"".equals(lid) ?Long.parseLong(lid):0);
        filesEntity.setSize(file.getSize());
        filesEntity.setPath(dateDir.getPath());
        filesEntity.setDownCounts(0);
        filesEntity.setType(file.getContentType());
        filesEntity.setOldFileName(file.getOriginalFilename());
        filesEntity.setNewFileName(FileUtil.newFileName(file));
        filesEntity.setUId(Long.parseLong(uid));
        filesEntity.setExtension( "."+ FilenameUtils.getExtension(file.getOriginalFilename()));
        file.transferTo(new File(dateDir,filesEntity.getNewFileName()));
    }
}
